package yc.web;

import yc.bean.PageBean;
import yc.bean.Resfood;
import yc.dao.DBHelper;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class ResfoodBiz {
    public PageBean findByPage(PageBean pageBean) throws Exception {
        List<Resfood> dataset=this.findByPage(pageBean.getPageno(),pageBean.getPagesize(),pageBean.getSortby(),pageBean.getSort());
        long total=this.countAll();
        pageBean.setTotal(total);
        pageBean.setDataset(dataset);
        //其他的分页数据
        //计算总页数
        long totalPages=total%pageBean.getPagesize()==0?total/pageBean.getPagesize():total/pageBean.getPagesize()+1;
        pageBean.setTotalpages((int)totalPages);
        //上一页页号的计算
        if (pageBean.getPageno()<=1){
            pageBean.setPre(1);
        }else {
            pageBean.setPre(pageBean.getPageno()-1);
        }
        //下一页的页号
        if (pageBean.getPageno()==totalPages){
            pageBean.setNext((int)totalPages);
        }else {
            pageBean.setNext(pageBean.getPageno()+1);
        }
        return pageBean;
    }
    //查询resfood的总记录数
    private Long countAll() throws Exception {
        String sql="select count(fid) from resfood";
        DBHelper db=new DBHelper();
        long l=(long)db.selectAggregation(sql);
        return l;
    }
    //记录当前页的数据
    private List<Resfood>findByPage(int pageno, int pagesize, String sortby, String sort) throws InvocationTargetException, InstantiationException, IllegalAccessException {
        String sql="select * from resfood order by ? ? limit ?,?";
        DBHelper db=new DBHelper();
        int start=(pageno-1)*pagesize;
        List<Resfood>  list=db.select(   Resfood.class,  sql,     sortby,sort,start,pagesize );
       // List<Resfood>list=db.select(sql,Resfood.class,sortby,sort,start,pagesize);
        return list;


    }
}
